package com.zhang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.BaseCategory3;
import com.zhang.entity.PlatformPropertyKey;
import com.zhang.entity.PlatformPropertyValue;
import com.zhang.result.RetVal;
import com.zhang.service.PlatformPropertyKeyService;
import com.zhang.service.PlatformPropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性表 前端控制器
 * </p>
 *
 * @author zbw
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/product")
//@CrossOrigin
public class PlatformPropertyController {
    @Autowired
    private PlatformPropertyKeyService platformPropertyKeyService;
    @Autowired
    private PlatformPropertyValueService propertyValueService;

    //1.根据商品分类id查询平台属性
    //http://127.0.0.1:8000/product/getPlatformPropertyByCategoryId/2/13/6
    @GetMapping("getPlatformPropertyByCategoryId/{category1Id}/{category2Id}/{category3Id}")
    public RetVal getPlatformPropertyByCategoryId(@PathVariable Long category1Id,
                                                  @PathVariable Long category2Id,
                                                  @PathVariable Long category3Id){
        List<PlatformPropertyKey> propertyKeyList=platformPropertyKeyService.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);
        return RetVal.ok(propertyKeyList);
    }

    //2.根据平台属性key的id查询平台属性值
    //http://127.0.0.1:8000/product/getPropertyValueByPropertyKeyId/4
    @GetMapping("getPropertyValueByPropertyKeyId/{propertyKeyId}")
    public RetVal getPropertyValueByPropertyKeyId(@PathVariable Long propertyKeyId){
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id",propertyKeyId);
        List<PlatformPropertyValue> propertyValueList = propertyValueService.list(wrapper);
        return RetVal.ok(propertyValueList);
    }

    //3.保存/修改平台属性
    //http://127.0.0.1:8000/product/savePlatformProperty
    @PostMapping("savePlatformProperty")
    public RetVal savePlatformProperty(@RequestBody PlatformPropertyKey platformPropertyKey){
        platformPropertyKeyService.savePlatformProperty(platformPropertyKey);
        //针对当前是否保存成功的布尔值，进行返回结果的输出
        return RetVal.ok();
    }

    //4.根据skuId查询商品平台属性
    @GetMapping("getPlatformPropertyBySkuId/{skuId}")
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(@PathVariable Long skuId){
        return platformPropertyKeyService.getPlatformPropertyBySkuId(skuId);
    }
}

